package com.chuck.blogpro.controller;

import com.chuck.blogpro.pojo.Article;
import com.chuck.blogpro.pojo.PageBean;
import com.chuck.blogpro.pojo.Result;
import com.chuck.blogpro.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文章管理
 */
@Slf4j
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // 获取文章列表
    @GetMapping
    public Result article(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean = articleService.page(page, pageSize);
        return Result.success(pageBean);
    }

    // 获取文章详情
    @GetMapping("/{id}")
    public Result article(@PathVariable Integer id) {
        Article article = articleService.getArticleById(id);
        return Result.success(article);
    }

    // 添加文章
    @PostMapping
    public Result add(Article article) {
        articleService.add(article);
        return Result.success();
    }

    // 删除文章
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        articleService.delete(id);
        return Result.success();
    }

    // 修改文章
    @PutMapping
    public Result update(Article article) {
        articleService.update(article);
        return Result.success();
    }
}
